package controller;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.ChoiceQuestion;

import core.PgSQL;
import core.View;

public class QuestionThriller extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static PgSQL db = PgSQL.getInstance();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ResultSet rs = db.query("SELECT * FROM qz_choicequestion ORDER BY random() LIMIT 1");
		ChoiceQuestion q = null;
		try{
			if(rs.next()){
				q = ChoiceQuestion.fromDatabase(rs);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		request.setAttribute("question", q);
		if(request.getParameter("ajax") == null){
			View.show(request, response, "question_thriller");
		}else{
			View.show(request, response, "question_thriller_ajax");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
}
